home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / The World of Computer Software.iso / winsr173.zip / FRACSUBR.C < prev    next >
Text File  |  1992-01-26  |  35KB  |  1,095 lines

  1. /*
  2. FRACSUBR.C contains subroutines which belong primarily to CALCFRAC.C and
  3. FRACTALS.C, i.e. which are non-fractal-specific fractal engine subroutines.
  4. */
  5.  
  6. #include <stdio.h>
  7. #include <stdarg.h>
  8. #include <float.h>
  9. #include <sys\types.h>
  10. #include <sys\timeb.h>
  11. #include "fractint.h"
  12. #include "fractype.h"
  13. #include "mpmath.h"
  14.  
  15. /* routines in this module    */
  16.  
  17. void calcfracinit(void);
  18. void adjust_corner(void);
  19. int  alloc_resume(int,int);
  20. int  put_resume(int,...);
  21. int  start_resume(void);
  22. int  get_resume(int,...);
  23. void end_resume(void);
  24. void iplot_orbit(long,long,int);
  25. void plot_orbit(double,double,int);
  26. void scrub_orbit(void);
  27. int  add_worklist(int,int,int,int,int,int,int);
  28. void tidy_worklist(void);
  29. int  ssg_blocksize(void);
  30. void get_julia_attractor(double,double);
  31.  
  32. static long   _fastcall fudgetolong(double d);
  33. static double _fastcall fudgetodouble(long l);
  34. static void   _fastcall adjust_to_limits(double);
  35. static void   _fastcall smallest_add(double *);
  36. static int    _fastcall ratio_bad(double,double);
  37. static void   _fastcall plotdorbit(double,double,int);
  38. static int    _fastcall combine_worklist(void);
  39.  
  40.  
  41. extern int    calc_status;        /* status of calculations */
  42. extern char far *resume_info;        /* pointer to resume info if allocated */
  43.        int    resume_len;        /* length of resume info */
  44. static int    resume_offset;        /* offset in resume info gets */
  45. extern double plotmx1,plotmx2,plotmy1,plotmy2; /* real->screen conversion */
  46. extern int    orbit_ptr;        /* pointer into save_orbit array */
  47. extern int orbit_delay;         /* orbit delay value */
  48. extern int far *save_orbit;        /* array to save orbit values */
  49. extern int    orbit_color;        /* XOR color */
  50. extern int    num_worklist;        /* resume worklist for standard engine */
  51. extern int    fractype;         /* fractal type */
  52. extern char   stdcalcmode;        /* '1', '2', 'g', 'b'       */
  53. extern char   floatflag;        /* floating-point fractals? */
  54. extern int    integerfractal;        /* TRUE if fractal uses integer math */
  55. extern struct workliststuff worklist[MAXCALCWORK];
  56. extern int    sxdots,sydots;        /* # of dots on the physical screen    */
  57. extern int    sxoffs,syoffs;        /* physical top left of logical screen */
  58. extern int    xdots, ydots;        /* # of dots on the logical screen     */
  59. extern int    colors;            /* maximum colors available */
  60. extern long   fudge;            /* fudge factor (2**n) */
  61. extern int    bitshift;         /* bit shift for fudge */
  62. extern int    inside;            /* inside color: 1=blue     */
  63. extern int    outside;            /* outside color    */
  64. extern double xxmin,xxmax,yymin,yymax,xx3rd,yy3rd; /* corners */
  65. extern long   xmin, xmax, ymin, ymax, x3rd, y3rd;  /* integer equivs */
  66. extern int    maxit;            /* try this many iterations */
  67. extern int    attractors;        /* number of finite attractors    */
  68. extern struct complex  attr[];        /* finite attractor vals (f.p)    */
  69. extern struct lcomplex lattr[];     /* finite attractor vals (int)    */
  70. extern struct complex  old,new;
  71. extern struct lcomplex lold,lnew;
  72. extern double tempsqrx,tempsqry;
  73. extern long   ltempsqrx,ltempsqry;
  74. extern int    xxstart,xxstop;        /* these are same as worklist, */
  75. extern int    yystart,yystop,yybegin;    /* declared as separate items  */
  76. extern int    periodicitycheck;
  77. extern int    basin;
  78. extern int    finattract;
  79. extern int    pixelpi;            /* value of pi in pixels */
  80. extern double closenuff;
  81. extern long   lclosenuff;
  82. extern int    ixstart, ixstop, iystart, iystop;
  83. extern int    color;
  84. extern int    decomp[];
  85. extern double potparam[3];        /* three potential parameters*/
  86. extern int    distest;            /* non-zero if distance estimator */
  87. extern double param[4];         /* up to four parameters */
  88. extern int    invert;            /* non-zero if inversion active */
  89. extern int    biomorph,usr_biomorph;
  90. extern int    debugflag; /* internal use only - you didn't see this */
  91. extern long   creal, cimag;        /* for calcmand */
  92. extern long   delx, dely;        /* screen pixel increments  */
  93. extern long   delx2, dely2;        /* screen pixel increments  */
  94. extern double delxx, delyy;        /* screen pixel increments  */
  95. extern double delxx2, delyy2;        /* screen pixel increments  */
  96. extern long   delmin;            /* for calcfrac/calcmand    */
  97. extern double ddelmin;            /* same as a double        */
  98. extern int    potflag;            /* continuous potential flag */
  99. extern int    bailout;
  100. extern double rqlim;
  101. extern double  dxsize, dysize;        /* xdots-1, ydots-1        */
  102. extern int soundflag;
  103. extern int basehertz;
  104.  
  105. extern long   far *lx0, far *ly0;    /* x, y grid            */
  106. extern long   far *lx1, far *ly1;    /* adjustment for rotate    */
  107. /* note that lx1 & ly1 values can overflow into sign bit; since     */
  108. /* they're used only to add to lx0/ly0, 2s comp straightens it out  */
  109. extern double far *dx0, far *dy0;    /* floating pt equivs */
  110. extern double far *dx1, far *dy1;
  111.  
  112. extern char   usr_floatflag;
  113. extern char   usr_stdcalcmode;
  114. extern int    usr_periodicitycheck;
  115. extern int    usr_distest;
  116.  
  117. extern int StandardFractal(void);
  118. extern int calcmand(void);
  119. extern int calcmandfp(void);
  120.  
  121. #define FUDGEFACTOR    29    /* fudge all values up by 2**this */
  122. #define FUDGEFACTOR2    24    /* (or maybe this)          */
  123.  
  124.  
  125. void calcfracinit() /* initialize a *pile* of stuff for fractal calculation */
  126. {
  127.    int i;
  128.    double ftemp;
  129.  
  130.    floatflag = usr_floatflag;
  131.    if (calc_status == 2) /* on resume, ensure floatflag correct */
  132.       if (curfractalspecific->isinteger)
  133.      floatflag = 0;
  134.       else
  135.      floatflag = 1;
  136.  
  137. init_restart:
  138.  
  139.    /* the following variables may be forced to a different setting due to
  140.       calc routine constraints;  usr_xxx is what the user last said is wanted,
  141.       xxx is what we actually do in the current situation */
  142.    stdcalcmode        = usr_stdcalcmode;
  143.    periodicitycheck = usr_periodicitycheck;
  144.    distest        = usr_distest;
  145.    biomorph        = usr_biomorph;
  146.  
  147.    potflag = 0;
  148.    if (potparam[0] != 0.0
  149.      && colors >= 256
  150.      && (curfractalspecific->calctype == StandardFractal
  151.      || curfractalspecific->calctype == calcmand
  152.      || curfractalspecific->calctype == calcmandfp)) {
  153.       potflag = 1;
  154.       distest = 0;    /* can't do distest too */
  155.       }
  156.  
  157.    if (distest)
  158.       floatflag = 1;  /* force floating point for dist est */
  159.  
  160.    if (floatflag) { /* ensure type matches floatflag */
  161.       if (curfractalspecific->isinteger != 0
  162.     && curfractalspecific->tofloat != NOFRACTAL)
  163.      fractype = curfractalspecific->tofloat;
  164.       }
  165.    else {
  166.       if (curfractalspecific->isinteger == 0
  167.     && curfractalspecific->tofloat != NOFRACTAL)
  168.      fractype = curfractalspecific->tofloat;
  169.       }
  170.    curfractalspecific = &fractalspecific[fractype];
  171.  
  172.    integerfractal = curfractalspecific->isinteger;
  173.  
  174.    if (fractype == JULIBROT)
  175.       rqlim = 4;
  176.    else if (potflag && potparam[2] != 0.0)
  177.       rqlim = potparam[2];
  178. /* else if (decomp[0] > 0 && decomp[1] > 0)
  179.       rqlim = (double)decomp[1]; */
  180.    else if (bailout) /* user input bailout */
  181.       rqlim = bailout;
  182.    else if (biomorph != -1) /* biomorph benefits from larger bailout */
  183.       rqlim = 100;
  184.    else
  185.       rqlim = curfractalspecific->orbit_bailout;
  186.    if (integerfractal) /* the bailout limit mustn't be too high here */
  187.       if (rqlim > 127.0) rqlim = 127.0;
  188.  
  189.    if ((curfractalspecific->flags&NOROTATE) != 0) {
  190.       /* ensure min<max and unrotated rectangle */
  191.       if (xxmin > xxmax) { ftemp = xxmax; xxmax = xxmin; xxmin = ftemp; }
  192.       if (yymin > yymax) { ftemp = yymax; yymax = yymin; yymin = ftemp; }
  193.       xx3rd = xxmin; yy3rd = yymin;
  194.       }
  195.  
  196.    /* set up bitshift for integer math */
  197.    bitshift = FUDGEFACTOR2; /* by default, the smaller shift */
  198.    if (integerfractal > 1)  /* use specific override from table */
  199.       bitshift = integerfractal;
  200.    if (integerfractal == 0) /* float? */
  201.       if ((i = curfractalspecific->tofloat) != NOFRACTAL) /* -> int? */
  202.      if (fractalspecific[i].isinteger > 1) /* specific shift? */
  203.         bitshift = fractalspecific[i].isinteger;
  204.  
  205.    if (fractype == MANDEL || fractype == JULIA) { /* adust shift bits if.. */
  206.       if (potflag == 0                  /* not using potential */
  207.     && (fractype != MANDEL              /* and not an int mandel */
  208.         || (param[0] == 0.0 && param[1] == 0.0))  /* w/ "fudged" params */
  209.     && !invert                  /* and not inverting */
  210.     && biomorph == -1              /* and not biomorphing */
  211.     && rqlim <= 4.0               /* and bailout not too high */
  212.     && (outside > -2 || outside < -5)      /* and no funny outside stuff */
  213.     && debugflag != 1234)              /* and not debugging */
  214.      bitshift = FUDGEFACTOR;          /* use the larger bitshift */
  215.       if (param[0] < -1.99 || param[0] > 1.99) param[0] = 1.99;
  216.       if (param[1] < -1.99 || param[1] > 1.99) param[1] = 1.99;
  217.       }
  218.  
  219.    fudge = 1L << bitshift;
  220.  
  221.    l_at_rad = fudge/32768L;
  222.    f_at_rad = 1.0/32768L;
  223.  
  224.    /* now setup arrays of real coordinates corresponding to each pixel */
  225.  
  226.    adjust_to_limits(1.0); /* make sure all corners in valid range */
  227.  
  228.    delxx  = (xxmax - xx3rd) / dxsize; /* calculate stepsizes */
  229.    delyy  = (yymax - yy3rd) / dysize;
  230.    delxx2 = (xx3rd - xxmin) / dysize;
  231.    delyy2 = (yy3rd - yymin) / dxsize;
  232.  
  233.    creal = fudgetolong(param[0]); /* integer equivs for it all */
  234.    cimag = fudgetolong(param[1]);
  235.    xmin  = fudgetolong(xxmin);
  236.    xmax  = fudgetolong(xxmax);
  237.    x3rd  = fudgetolong(xx3rd);
  238.    ymin  = fudgetolong(yymin);
  239.    ymax  = fudgetolong(yymax);
  240.    y3rd  = fudgetolong(yy3rd);
  241.    delx  = fudgetolong(delxx);
  242.    dely  = fudgetolong(delyy);
  243.    delx2 = fudgetolong(delxx2);
  244.    dely2 = fudgetolong(delyy2);
  245.  
  246.    if (fractype != PLASMA) { /* skip this if plasma to avoid 3d problems */
  247.       if (integerfractal && !invert) {
  248.      if (    (delx  == 0 && delxx  != 0.0)
  249.          || (delx2 == 0 && delxx2 != 0.0)
  250.          || (dely  == 0 && delyy  != 0.0)
  251.          || (dely2 == 0 && delyy2 != 0.0) )
  252.         goto expand_retry;
  253.      lx0[0] = xmin;         /* fill up the x, y grids */
  254.      ly0[0] = ymax;
  255.      lx1[0] = ly1[0] = 0;
  256.      for (i = 1; i < xdots; i++ ) {
  257.         lx0[i] = lx0[i-1] + delx;
  258.         ly1[i] = ly1[i-1] - dely2;
  259.         }
  260.      for (i = 1; i < ydots; i++ ) {
  261.         ly0[i] = ly0[i-1] - dely;
  262.         lx1[i] = lx1[i-1] + delx2;
  263.         }
  264.      /* past max res?  check corners within 10% of expected */
  265.      if (    ratio_bad((double)lx0[xdots-1]-xmin,(double)xmax-x3rd)
  266.          || ratio_bad((double)ly0[ydots-1]-ymax,(double)y3rd-ymax)
  267.          || ratio_bad((double)lx1[(ydots>>1)-1],((double)x3rd-xmin)/2)
  268.          || ratio_bad((double)ly1[(xdots>>1)-1],((double)ymin-y3rd)/2) ) {
  269. expand_retry:
  270.         if (integerfractal        /* integer fractal type? */
  271.           && curfractalspecific->tofloat != NOFRACTAL)
  272.            floatflag = 1;        /* switch to floating pt */
  273.         else
  274.            adjust_to_limits(2.0);    /* double the size */
  275.         if (calc_status == 2)    /* due to restore of an old file? */
  276.            calc_status = 0;     /*   whatever, it isn't resumable */
  277.         goto init_restart;
  278.         }
  279.      /* re-set corners to match reality */
  280.      xmax = lx0[xdots-1] + lx1[ydots-1];
  281.      ymin = ly0[ydots-1] + ly1[xdots-1];
  282.      x3rd = xmin + lx1[ydots-1];
  283.      y3rd = ly0[ydots-1];
  284.      xxmin = fudgetodouble(xmin);
  285.      xxmax = fudgetodouble(xmax);
  286.      xx3rd = fudgetodouble(x3rd);
  287.      yymin = fudgetodouble(ymin);
  288.      yymax = fudgetodouble(ymax);
  289.      yy3rd = fudgetodouble(y3rd);
  290.      }
  291.       else {
  292.      /* set up dx0 and dy0 analogs of lx0 and ly0 */
  293.      /* put fractal parameters in doubles */
  294.      dx0[0] = xxmin;        /* fill up the x, y grids */
  295.      dy0[0] = yymax;
  296.      dx1[0] = dy1[0] = 0;
  297.      for (i = 1; i < xdots; i++ ) {
  298.         dx0[i] = dx0[i-1] + delxx;
  299.         dy1[i] = dy1[i-1] - delyy2;
  300.         }
  301.      for (i = 1; i < ydots; i++ ) {
  302.         dy0[i] = dy0[i-1] - delyy;
  303.         dx1[i] = dx1[i-1] + delxx2;
  304.         }
  305.      if (    ratio_bad(dx0[xdots-1]-xxmin,xxmax-xx3rd)
  306.          || ratio_bad(dy0[ydots-1]-yymax,yy3rd-yymax)
  307.          || ratio_bad(dx1[ydots-1],xx3rd-xxmin)
  308.          || ratio_bad(dy1[xdots-1],yymin-yy3rd))
  309.         goto expand_retry;
  310.      /* re-set corners to match reality */
  311.      xxmax = dx0[xdots-1] + dx1[ydots-1];
  312.      yymin = dy0[ydots-1] + dy1[xdots-1];
  313.      xx3rd = xxmin + dx1[ydots-1];
  314.      yy3rd = dy0[ydots-1];
  315.      }
  316.       }
  317.  
  318.    /* for periodicity close-enough, and for unity: */
  319.    /*      min(max(delx,delx2),max(dely,dely2)       */
  320.    ddelmin = fabs(delxx);
  321.    if (fabs(delxx2) > ddelmin)
  322.       ddelmin = fabs(delxx2);
  323.    if (fabs(delyy) > fabs(delyy2)) {
  324.       if (fabs(delyy) < ddelmin)
  325.      ddelmin = fabs(delyy);
  326.       }
  327.    else
  328.       if (fabs(delyy2) < ddelmin)
  329.      ddelmin = fabs(delyy2);
  330.    delmin = fudgetolong(ddelmin);
  331.  
  332.    /* calculate factors which plot real values to screen co-ords */
  333.    /* calcfrac.c plot_orbit routines have comments about this     */
  334.    ftemp = (0.0-delyy2) * delxx2 * dxsize * dysize
  335.        - (xxmax-xx3rd) * (yy3rd-yymax);
  336.    plotmx1 = delxx2 * dxsize * dysize / ftemp;
  337.    plotmx2 = (yy3rd-yymax) * dxsize / ftemp;
  338.    plotmy1 = (0.0-delyy2) * dxsize * dysize / ftemp;
  339.    plotmy2 = (xxmax-xx3rd) * dysize / ftemp;
  340.  
  341. }
  342.  
  343. static long _fastcall fudgetolong(double d)
  344. {
  345.    if ((d *= fudge) > 0) d += 0.5;
  346.    else          d -= 0.5;
  347.    return (long)d;
  348. }
  349.  
  350. static double _fastcall fudgetodouble(long l)
  351. {
  352.    char buf[30];
  353.    double d;
  354.    sprintf(buf,"%.9g",(double)l / fudge);
  355.    sscanf(buf,"%lg",&d);
  356.    return d;
  357. }
  358.  
  359.  
  360. void adjust_corner()
  361. {
  362.    /* make edges very near vert/horiz exact, to ditch rounding errs and */
  363.    /* to avoid problems when delta per axis makes too large a ratio    */
  364.    double ftemp,ftemp2;
  365.    if( (ftemp=fabs(xx3rd-xxmin)) < (ftemp2=fabs(xxmax-xx3rd)) ) {
  366.       if (ftemp*10000 < ftemp2 && yy3rd != yymax)
  367.      xx3rd = xxmin;
  368.       }
  369.    else if (ftemp2*10000 < ftemp && yy3rd != yymin)
  370.       xx3rd = xxmax;
  371.    if( (ftemp=fabs(yy3rd-yymin)) < (ftemp2=fabs(yymax-yy3rd)) ) {
  372.       if (ftemp*10000 < ftemp2 && xx3rd != xxmax)
  373.      yy3rd = yymin;
  374.       }
  375.    else if (ftemp2*10000 < ftemp && xx3rd != xxmin)
  376.       yy3rd = yymax;
  377. }
  378.  
  379. static void _fastcall adjust_to_limits(double expand)
  380. {  double cornerx[4],cornery[4];
  381.    double lowx,highx,lowy,highy,limit,ftemp;
  382.    double centerx,centery,adjx,adjy;
  383.    int i;
  384.    limit = 32767.99;
  385.    if (bitshift >= 24) limit = 31.99;
  386.    if (bitshift >= 29) limit = 3.99;
  387.    centerx = (xxmin+xxmax)/2;
  388.    centery = (yymin+yymax)/2;
  389.    if (xxmin == centerx) { /* ohoh, infinitely thin, fix it */
  390.       smallest_add(&xxmax);
  391.       xxmin -= xxmax-centerx;
  392.       }
  393.    if (yymin == centery) {
  394.       smallest_add(&yymax);
  395.       yymin -= yymax-centery;
  396.       }
  397.    if (xx3rd == centerx)
  398.       smallest_add(&xx3rd);
  399.    if (yy3rd == centery)
  400.       smallest_add(&yy3rd);
  401.    /* setup array for easier manipulation */
  402.    cornerx[0] = xxmin; cornerx[1] = xxmax;
  403.    cornerx[2] = xx3rd; cornerx[3] = xxmin+(xxmax-xx3rd);
  404.    cornery[0] = yymax; cornery[1] = yymin;
  405.    cornery[2] = yy3rd; cornery[3] = yymin+(yymax-yy3rd);
  406.    /* if caller wants image size adjusted, do that first */
  407.    if (expand != 1.0)
  408.       for (i=0; i<4; ++i) {
  409.      cornerx[i] = centerx + (cornerx[i]-centerx)*expand;
  410.      cornery[i] = centery + (cornery[i]-centery)*expand;
  411.       }
  412.    /* get min/max x/y values */
  413.    lowx = highx = cornerx[0];
  414.    lowy = highy = cornery[0];
  415.    for (i=1; i<4; ++i) {
  416.       if (cornerx[i] < lowx)  lowx  = cornerx[i];
  417.       if (cornerx[i] > highx) highx = cornerx[i];
  418.       if (cornery[i] < lowy)  lowy  = cornery[i];
  419.       if (cornery[i] > highy) highy = cornery[i];
  420.       }
  421.    /* if image is too large, downsize it maintaining center */
  422.    ftemp = highx-lowx;
  423.    if (highy-lowy > ftemp) ftemp = highy-lowy;
  424.    if ((ftemp = limit*2/ftemp) < 1.0)
  425.       for (i=0; i<4; ++i) {
  426.      cornerx[i] = centerx + (cornerx[i]-centerx)*ftemp;
  427.      cornery[i] = centery + (cornery[i]-centery)*ftemp;
  428.      }
  429.    /* if any corner has x or y past limit, move the image */
  430.    adjx = adjy = 0;
  431.    for (i=0; i<4; ++i) {
  432.       if (cornerx[i] > limit     && (ftemp = cornerx[i] - limit) > adjx)
  433.      adjx = ftemp;
  434.       if (cornerx[i] < 0.0-limit && (ftemp = cornerx[i] + limit) < adjx)
  435.      adjx = ftemp;
  436.       if (cornery[i] > limit     && (ftemp = cornery[i] - limit) > adjy)
  437.      adjy = ftemp;
  438.       if (cornery[i] < 0.0-limit && (ftemp = cornery[i] + limit) < adjy)
  439.      adjy = ftemp;
  440.       }
  441.    if (calc_status == 2 && (adjx != 0 || adjy != 0))
  442.       calc_status = 0;
  443.    xxmin = cornerx[0] - adjx;
  444.    xxmax = cornerx[1] - adjx;
  445.    xx3rd = cornerx[2] - adjx;
  446.    yymax = cornery[0] - adjy;
  447.    yymin = cornery[1] - adjy;
  448.    yy3rd = cornery[2] - adjy;
  449.    adjust_corner(); /* make 3rd corner exact if very near other co-ords */
  450. }
  451.  
  452. static void _fastcall smallest_add(double *num)
  453. {
  454.    *num += *num * 5.0e-16;
  455. }
  456.  
  457. static int _fastcall ratio_bad(double actual, double desired)
  458. {  double ftemp;
  459.    if (desired != 0)
  460.       if ((ftemp = actual / desired) < 0.95 || ftemp > 1.05)
  461.      return(1);
  462.    return(0);
  463. }
  464.  
  465.  
  466. /* Save/resume stuff:
  467.  
  468.    Engines which aren't resumable can simply ignore all this.
  469.  
  470.    Before calling the (per_image,calctype) routines (engine), calcfract sets:
  471.       "resuming" to 0 if new image, nonzero if resuming a partially done image
  472.       "calc_status" to 1
  473.    If an engine is interrupted and wants to be able to resume it must:
  474.       store whatever status info it needs to be able to resume later
  475.       set calc_status to 2 and return
  476.    If subsequently called with resuming!=0, the engine must restore status
  477.    info and continue from where it left off.
  478.  
  479.    Since the info required for resume can get rather large for some types,
  480.    it is not stored directly in save_info.  Instead, memory is dynamically
  481.    allocated as required, and stored in .fra files as a separate block.
  482.    To save info for later resume, an engine routine can use:
  483.       alloc_resume(maxsize,version)
  484.      Maxsize must be >= max bytes subsequently saved + 2; over-allocation
  485.      is harmless except for possibility of insufficient mem available;
  486.      undersize is not checked and probably causes serious misbehaviour.
  487.      Version is an arbitrary number so that subsequent revisions of the
  488.      engine can be made backward compatible.
  489.      Alloc_resume sets calc_status to 2 (resumable) if it succeeds; to 3
  490.      if it cannot allocate memory (and issues warning to user).
  491.       put_resume({bytes,&argument,} ... 0)
  492.      Can be called as often as required to store the info.
  493.      Arguments must not be far addresses.
  494.      Is not protected against calls which use more space than allocated.
  495.    To reload info when resuming, use:
  496.       start_resume()
  497.      initializes and returns version number
  498.       get_resume({bytes,&argument,} ... 0)
  499.      inverse of store_resume
  500.       end_resume()
  501.      optional, frees the memory area sooner than would happen otherwise
  502.  
  503.    Example, save info:
  504.       alloc_resume(sizeof(parmarray)+100,2);
  505.       put_resume(sizeof(int),&row, sizeof(int),&col,
  506.          sizeof(parmarray),parmarray, 0);
  507.     restore info:
  508.       vsn = start_resume();
  509.       get_resume(sizeof(int),&row, sizeof(int),&col, 0);
  510.       if (vsn >= 2)
  511.      get_resume(sizeof(parmarray),parmarray,0);
  512.       end_resume();
  513.  
  514.    Engines which allocate a large far memory chunk of their own might
  515.    directly set resume_info, resume_len, calc_status to avoid doubling
  516.    transient memory needs by using these routines.
  517.  
  518.    StandardFractal, calcmand, solidguess, and bound_trace_main are a related
  519.    set of engines for escape-time fractals.  They use a common worklist
  520.    structure for save/resume.  Fractals using these must specify calcmand
  521.    or StandardFractal as the engine in fractalspecificinfo.
  522.    Other engines don't get btm nor ssg, don't get off-axis symmetry nor
  523.    panning (the worklist stuff), and are on their own for save/resume.
  524.  
  525.    */
  526.  
  527. int put_resume(int len, ...)
  528. {
  529.    va_list arg_marker;    /* variable arg list */
  530.    char *source_ptr;
  531.    if (resume_info == NULL)
  532.       return(-1);
  533.    va_start(arg_marker,len);
  534.    while (len)
  535.    {
  536.       source_ptr = va_arg(arg_marker,char *);
  537.       far_memcpy(resume_info+resume_len,source_ptr,len);
  538.       resume_len += len;
  539.       len = va_arg(arg_marker,int);
  540.    }
  541.    return(0);
  542. }
  543.  
  544. int alloc_resume(int alloclen, int version)
  545. {
  546.    if (resume_info != NULL) /* free the prior area if there is one */
  547.       farmemfree(resume_info);
  548.    if ((resume_info = farmemalloc((long)alloclen))== NULL)
  549.    {
  550.       static char msg[] = {"\
  551. Warning - insufficient free memory to save status.\n\
  552. You will not be able to resume calculating this image."};
  553.       stopmsg(0,msg);
  554.       calc_status = 3;
  555.       return(-1);
  556.    }
  557.    resume_len = 0;
  558.    put_resume(sizeof(int),&version,0);
  559.    calc_status = 2;
  560.    return(0);
  561. }
  562.  
  563. int get_resume(int len, ...)
  564. {
  565.    va_list arg_marker;    /* variable arg list */
  566.    char *dest_ptr;
  567.    if (resume_info == NULL)
  568.       return(-1);
  569.    va_start(arg_marker,len);
  570.    while (len)
  571.    {
  572.       dest_ptr = va_arg(arg_marker,char *);
  573.       far_memcpy(dest_ptr,resume_info+resume_offset,len);
  574.       resume_offset += len;
  575.       len = va_arg(arg_marker,int);
  576.    }
  577.    return(0);
  578. }
  579.  
  580. int start_resume()
  581. {
  582.    int version;
  583.    if (resume_info == NULL)
  584.       return(-1);
  585.    resume_offset = 0;
  586.    get_resume(sizeof(int),&version,0);
  587.    return(version);
  588. }
  589.  
  590. void end_resume()
  591. {
  592.    if (resume_info != NULL) /* free the prior area if there is one */
  593.    {
  594.       farmemfree(resume_info);
  595.       resume_info = NULL;
  596.    }
  597. }
  598.  
  599.  
  600. /* Showing orbit requires converting real co-ords to screen co-ords.
  601.    Define:
  602.        Xs == xxmax-xx3rd           Ys == yy3rd-yymax
  603.        W  == xdots-1               D  == ydots-1
  604.    We know that:
  605.        realx == lx0[col] + lx1[row]
  606.        realy == ly0[row] + ly1[col]
  607.        lx0[col] == (col/width) * Xs + xxmin
  608.        lx1[row] == row * delxx
  609.        ly0[row] == (row/D) * Ys + yymax
  610.        ly1[col] == col * (0-delyy)
  611.   so:
  612.        realx == (col/W) * Xs + xxmin + row * delxx
  613.        realy == (row/D) * Ys + yymax + col * (0-delyy)
  614.   and therefore:
  615.        row == (realx-xxmin - (col/W)*Xs) / Xv     (1)
  616.        col == (realy-yymax - (row/D)*Ys) / Yv     (2)
  617.   substitute (2) into (1) and solve for row:
  618.        row == ((realx-xxmin)*(0-delyy2)*W*D - (realy-yymax)*Xs*D)
  619.               / ((0-delyy2)*W*delxx2*D-Ys*Xs)
  620.   */
  621.  
  622. /* sleep N * a tenth of a millisecond */
  623.  
  624. void sleepms(long ms)
  625. {
  626.     extern int tabmode;
  627.     static long scalems=0L;
  628.     int savehelpmode,savetabmode;
  629.     struct timeb t1,t2;
  630. #define SLEEPINIT 250 /* milliseconds for calibration */
  631.     savetabmode  = tabmode;
  632.     savehelpmode = helpmode;
  633.     tabmode  = 0;
  634.     helpmode = -1;
  635.     if(scalems==0L) /* calibrate */
  636.     {
  637.     /* selects a value of scalems that makes the units
  638.        10000 per sec independent of CPU speed */
  639.     char msg[80];
  640.     long millisecs;
  641.     int i,elapsed;
  642.  
  643.     scalems = 1L;
  644.     if(keypressed()) /* check at start, hope to get start of timeslice */
  645.        goto sleepexit;
  646.     /* calibrate, assume slow computer first */
  647.     do
  648.     {
  649.        scalems *= 2;
  650.        ftime(&t2);
  651.        do { /* wait for the start of a new tick */
  652.           ftime(&t1);
  653.         }
  654.         while (t2.time == t1.time && t2.millitm == t1.millitm);
  655.        sleepms(10L * SLEEPINIT); /* about 1/4 sec */
  656.        ftime(&t2);
  657.        if(keypressed()) {
  658.           scalems = 0L;
  659.           goto sleepexit;
  660.        }
  661.      }
  662.      while ((elapsed = (int)(t2.time-t1.time)*1000 + t2.millitm-t1.millitm)
  663.         < SLEEPINIT);
  664.     /* once more to see if faster (eg multi-tasking) */
  665.     do { /* wait for the start of a new tick */
  666.        ftime(&t1);
  667.        }
  668.      while (t2.time == t1.time && t2.millitm == t1.millitm);
  669.     sleepms(10L * SLEEPINIT);
  670.     ftime(&t2);
  671.     if ((i = (int)(t2.time-t1.time)*1000 + t2.millitm-t1.millitm) < elapsed)
  672.        elapsed = (i == 0) ? 1 : i;
  673.     scalems = (float)SLEEPINIT/(float)(elapsed) * scalems;
  674.     if (debugflag == 700) {
  675.        sprintf(msg,"scale factor=%ld",scalems);
  676.        stopmsg(0,msg);
  677.     }
  678.     }
  679.     if(ms > 10L * SLEEPINIT) { /* using ftime is probably more accurate */
  680.     ms /= 10;
  681.     ftime(&t1);
  682.     while(1) {
  683.        if(keypressed()) break;
  684.        ftime(&t2);
  685.        if ((t2.time-t1.time)*1000 + t2.millitm-t1.millitm >= ms) break;
  686.     }
  687.     }
  688.     else
  689.     if(!keypressed()) {
  690.        ms *= scalems;
  691.        while(ms-- >= 0);
  692.     }
  693. sleepexit:
  694.     tabmode  = savetabmode;
  695.     helpmode = savehelpmode;
  696. }
  697.  
  698.  
  699.  
  700. static void _fastcall plotdorbit(double dx, double dy, int color)
  701. {
  702.    int i, j, c;
  703.    int save_sxoffs,save_syoffs;
  704.    if (orbit_ptr >= 1500) return;
  705.    i = dy * plotmx1 - dx * plotmx2; i += sxoffs;
  706.    if (i < 0 || i >= sxdots) return;
  707.    j = dx * plotmy1 - dy * plotmy2; j += syoffs;
  708.    if (j < 0 || j >= sydots) return;
  709.    save_sxoffs = sxoffs;
  710.    save_syoffs = syoffs;
  711.    sxoffs = syoffs = 0;
  712.    /* save orbit value */
  713.    if(color == -1)
  714.    {
  715.       *(save_orbit + orbit_ptr++) = i;
  716.       *(save_orbit + orbit_ptr++) = j;
  717.       *(save_orbit + orbit_ptr++) = c = getcolor(i,j);
  718.       putcolor(i,j,c^orbit_color);
  719.    }
  720.    else
  721.       putcolor(i,j,color);
  722.    sxoffs = save_sxoffs;
  723.    syoffs = save_syoffs;
  724.    if(orbit_delay > 0)
  725.       sleepms(orbit_delay);
  726.    if(soundflag==1)
  727.        snd((int)(i*1000/xdots+basehertz));
  728.    else if(soundflag > 1)
  729.        snd((int)(j*1000/ydots+basehertz));
  730.    /* placing sleepms here delays each dot */
  731. }
  732.  
  733. void iplot_orbit(ix, iy, color)
  734. long ix, iy;
  735. int color;
  736. {
  737.    plotdorbit((double)ix/fudge-xxmin,(double)iy/fudge-yymax,color);
  738. }
  739.  
  740. void plot_orbit(real,imag,color)
  741. double real,imag;
  742. int color;
  743. {
  744.    plotdorbit(real-xxmin,imag-yymax,color);
  745. }
  746.  
  747. void scrub_orbit()
  748. {
  749.    static long oldtime = 0;
  750.    int i,j,c;
  751.    int save_sxoffs,save_syoffs;
  752.    save_sxoffs = sxoffs;
  753.    save_syoffs = syoffs;
  754.    sxoffs = syoffs = 0;
  755.    while(orbit_ptr > 0)
  756.    {
  757.       c = *(save_orbit + --orbit_ptr);
  758.       j = *(save_orbit + --orbit_ptr);
  759.       i = *(save_orbit + --orbit_ptr);
  760.       putcolor(i,j,c);
  761.    }
  762.    sxoffs = save_sxoffs;
  763.    syoffs = save_syoffs;
  764.    nosnd();
  765. }
  766.  
  767.  
  768. int add_worklist(int xfrom, int xto, int yfrom, int yto, int ybegin,
  769. int pass, int sym)
  770. {
  771.    if (num_worklist >= MAXCALCWORK)
  772.       return(-1);
  773.    worklist[num_worklist].xxstart = xfrom;
  774.    worklist[num_worklist].xxstop  = xto;
  775.    worklist[num_worklist].yystart = yfrom;
  776.    worklist[num_worklist].yystop  = yto;
  777.    worklist[num_worklist].yybegin = ybegin;
  778.    worklist[num_worklist].pass      = pass;
  779.    worklist[num_worklist].sym      = sym;
  780.    ++num_worklist;
  781.    tidy_worklist();
  782.    return(0);
  783. }
  784.  
  785. static int _fastcall combine_worklist() /* look for 2 entries which can freely merge */
  786. {
  787.    int i,j;
  788.    for (i=0; i<num_worklist; ++i)
  789.       if (worklist[i].yystart == worklist[i].yybegin)
  790.      for (j=i+1; j<num_worklist; ++j)
  791.         if (worklist[j].sym == worklist[i].sym
  792.         && worklist[j].yystart == worklist[j].yybegin
  793.         && worklist[i].pass == worklist[j].pass)
  794.         {
  795.            if ( worklist[i].xxstart == worklist[j].xxstart
  796.            && worklist[i].xxstop  == worklist[j].xxstop)
  797.            {
  798.           if (worklist[i].yystop+1 == worklist[j].yystart)
  799.           {
  800.              worklist[i].yystop = worklist[j].yystop;
  801.              return(j);
  802.           }
  803.           if (worklist[j].yystop+1 == worklist[i].yystart)
  804.           {
  805.              worklist[i].yystart = worklist[j].yystart;
  806.              worklist[i].yybegin = worklist[j].yybegin;
  807.              return(j);
  808.           }
  809.            }
  810.            if ( worklist[i].yystart == worklist[j].yystart
  811.            && worklist[i].yystop  == worklist[j].yystop)
  812.            {
  813.           if (worklist[i].xxstop+1 == worklist[j].xxstart)
  814.           {
  815.              worklist[i].xxstop = worklist[j].xxstop;
  816.              return(j);
  817.           }
  818.           if (worklist[j].xxstop+1 == worklist[i].xxstart)
  819.           {
  820.              worklist[i].xxstart = worklist[j].xxstart;
  821.              return(j);
  822.           }
  823.            }
  824.         }
  825.    return(0); /* nothing combined */
  826. }
  827.  
  828. void tidy_worklist() /* combine mergeable entries, resort */
  829. {
  830.    int i,j;
  831.    struct workliststuff tempwork;
  832.    while (i=combine_worklist())
  833.    { /* merged two, delete the gone one */
  834.       while (++i < num_worklist)
  835.      worklist[i-1] = worklist[i];
  836.       --num_worklist;
  837.    }
  838.    for (i=0; i<num_worklist; ++i)
  839.       for (j=i+1; j<num_worklist; ++j)
  840.      if (worklist[j].pass < worklist[i].pass
  841.          || (worklist[j].pass == worklist[i].pass
  842.          && (worklist[j].yystart < worklist[i].yystart
  843.          || (   worklist[j].yystart == worklist[i].yystart
  844.          && worklist[j].xxstart <  worklist[i].xxstart))))
  845.      { /* dumb sort, swap 2 entries to correct order */
  846.         tempwork = worklist[i];
  847.         worklist[i] = worklist[j];
  848.         worklist[j] = tempwork;
  849.      }
  850. }
  851.  
  852.  
  853. void get_julia_attractor (double real, double imag)
  854. {
  855.    struct lcomplex lresult;
  856.    struct complex result;
  857.    int savper,savmaxit;
  858.  
  859.    if (attractors == 0 && finattract == 0) /* not magnet & not requested */
  860.       return;
  861.  
  862.    if (attractors >= N_ATTR)     /* space for more attractors ?  */
  863.       return;               /* Bad luck - no room left !    */
  864.  
  865.    savper = periodicitycheck;
  866.    savmaxit = maxit;
  867.    periodicitycheck = 0;
  868.    old.x = real;            /* prepare for f.p orbit calc */
  869.    old.y = imag;
  870.    tempsqrx = sqr(old.x);
  871.    tempsqry = sqr(old.y);
  872.  
  873.    lold.x = real;        /* prepare for int orbit calc */
  874.    lold.y = imag;
  875.    ltempsqrx = tempsqrx;
  876.    ltempsqry = tempsqry;
  877.  
  878.    lold.x = lold.x << bitshift;
  879.    lold.y = lold.y << bitshift;
  880.    ltempsqrx = ltempsqrx << bitshift;
  881.    ltempsqry = ltempsqry << bitshift;
  882.  
  883.    if (maxit < 500)        /* we're going to try at least this hard */
  884.       maxit = 500;
  885.    color = 0;
  886.    while (++color < maxit)
  887.       if(curfractalspecific->orbitcalc())
  888.      break;
  889.    if (color >= maxit)        /* if orbit stays in the lake */
  890.    {
  891.       if (integerfractal)   /* remember where it went to */
  892.      lresult = lnew;
  893.       else
  894.      result =  new;
  895.       if(!curfractalspecific->orbitcalc()) /* if it stays in the lake */
  896.       {                /* and doen't move far, probably */
  897.      if (integerfractal)   /*   found a finite attractor    */
  898.      {
  899.         if(labs(lresult.x-lnew.x) < lclosenuff
  900.         && labs(lresult.y-lnew.y) < lclosenuff)
  901.         {
  902.            lattr[attractors] = lnew;
  903.            attractors++;   /* another attractor - coloured lakes ! */
  904.         }
  905.      }
  906.      else
  907.      {
  908.         if(fabs(result.x-new.x) < closenuff
  909.         && fabs(result.y-new.y) < closenuff)
  910.         {
  911.            attr[attractors] = new;
  912.            attractors++;   /* another attractor - coloured lakes ! */
  913.         }
  914.      }
  915.       }
  916.    }
  917.    if(attractors==0)
  918.       periodicitycheck = savper;
  919.    maxit = savmaxit;
  920. }
  921.  
  922.  
  923. #define maxyblk 7    /* must match calcfrac.c */
  924. #define maxxblk 202  /* must match calcfrac.c */
  925. int ssg_blocksize() /* used by solidguessing and by zoom panning */
  926. {
  927.    int blocksize,i;
  928.    /* blocksize 4 if <300 rows, 8 if 300-599, 16 if 600-1199, 32 if >=1200 */
  929.    blocksize=4;
  930.    i=300;
  931.    while(i<=ydots)
  932.    {
  933.       blocksize+=blocksize;
  934.       i+=i;
  935.    }
  936.    /* increase blocksize if prefix array not big enough */
  937.    while(blocksize*(maxxblk-2)<xdots || blocksize*(maxyblk-2)*16<ydots)
  938.       blocksize+=blocksize;
  939.    return(blocksize);
  940. }
  941.  
  942.  
  943. /* Symmetry plot for period PI */
  944. void _fastcall symPIplot(x, y, color)
  945. int x, y, color ;
  946. {
  947.    while(x <= xxstop)
  948.    {
  949.       putcolor(x, y, color) ;
  950.       x += pixelpi;
  951.    }
  952. }
  953. /* Symmetry plot for period PI plus Origin Symmetry */
  954. void _fastcall symPIplot2J(x, y, color)
  955. int x, y, color ;
  956. {
  957.    int i,j;
  958.    while(x <= xxstop)
  959.    {
  960.       putcolor(x, y, color) ;
  961.       if ((i=yystop-(y-yystart)) > iystop && i < ydots
  962.       && (j=xxstop-(x-xxstart)) < xdots)
  963.      putcolor(j, i, color) ;
  964.       x += pixelpi;
  965.    }
  966. }
  967. /* Symmetry plot for period PI plus Both Axis Symmetry */
  968. void _fastcall symPIplot4J(x, y, color)
  969. int x, y, color ;
  970. {
  971.    int i,j;
  972.    while(x <= (xxstart+xxstop)/2)
  973.    {
  974.       j = xxstop-(x-xxstart);
  975.       putcolor(       x , y , color) ;
  976.       if (j < xdots)
  977.      putcolor(j , y , color) ;
  978.       if ((i=yystop-(y-yystart)) > iystop && i < ydots)
  979.       {
  980.      putcolor(x , i , color) ;
  981.      if (j < xdots)
  982.         putcolor(j , i , color) ;
  983.       }
  984.       x += pixelpi;
  985.    }
  986. }
  987.  
  988. /* Symmetry plot for X Axis Symmetry */
  989. void _fastcall symplot2(x, y, color)
  990. int x, y, color ;
  991. {
  992.    int i;
  993.    putcolor(x, y, color) ;
  994.    if ((i=yystop-(y-yystart)) > iystop && i < ydots)
  995.       putcolor(x, i, color) ;
  996. }
  997.  
  998. /* Symmetry plot for Y Axis Symmetry */
  999. void _fastcall symplot2Y(x, y, color)
  1000. int x, y, color ;
  1001. {
  1002.    int i;
  1003.    putcolor(x, y, color) ;
  1004.    if ((i=xxstop-(x-xxstart)) < xdots)
  1005.       putcolor(i, y, color) ;
  1006. }
  1007.  
  1008. /* Symmetry plot for Origin Symmetry */
  1009. void _fastcall symplot2J(x, y, color)
  1010. int x, y, color ;
  1011. {
  1012.    int i,j;
  1013.    putcolor(x, y, color) ;
  1014.    if ((i=yystop-(y-yystart)) > iystop && i < ydots
  1015.        && (j=xxstop-(x-xxstart)) < xdots)
  1016.       putcolor(j, i, color) ;
  1017. }
  1018.  
  1019. /* Symmetry plot for Both Axis Symmetry */
  1020. void _fastcall symplot4(x, y, color)
  1021. int x, y, color ;
  1022. {
  1023.    int i,j;
  1024.    j = xxstop-(x-xxstart);
  1025.    putcolor(       x , y, color) ;
  1026.    if (j < xdots)
  1027.       putcolor(    j , y, color) ;
  1028.    if ((i=yystop-(y-yystart)) > iystop && i < ydots)
  1029.    {
  1030.       putcolor(    x , i, color) ;
  1031.       if (j < xdots)
  1032.      putcolor(j , i, color) ;
  1033.    }
  1034. }
  1035.  
  1036. /* Symmetry plot for X Axis Symmetry - Striped Newtbasin version */
  1037. void _fastcall symplot2basin(x, y, color)
  1038. int x, y, color ;
  1039. {
  1040.    int i,stripe;
  1041.    extern int degree;
  1042.    putcolor(x, y, color) ;
  1043.    if(basin==2 && color > 8)
  1044.       stripe=8;
  1045.    else
  1046.       stripe = 0;
  1047.    if ((i=yystop-(y-yystart)) > iystop && i < ydots)
  1048.    {
  1049.       color -= stripe;              /* reconstruct unstriped color */
  1050.       color = (degree+1-color)%degree+1;  /* symmetrical color */
  1051.       color += stripe;              /* add stripe */
  1052.       putcolor(x, i,color)  ;
  1053.    }
  1054. }
  1055.  
  1056. /* Symmetry plot for Both Axis Symmetry  - Newtbasin version */
  1057. void _fastcall symplot4basin(x, y, color)
  1058. int x, y, color ;
  1059. {
  1060.    extern int degree;
  1061.    int i,j,color1,stripe;
  1062.    if(color == 0) /* assumed to be "inside" color */
  1063.    {
  1064.       symplot4(x, y, color);
  1065.       return;
  1066.    }
  1067.    if(basin==2 && color > 8)
  1068.       stripe = 8;
  1069.    else
  1070.       stripe = 0;
  1071.    color -= stripe;          /* reconstruct unstriped color */
  1072.    color1 = degree/2+degree+2 - color;
  1073.    if(color < degree/2+2)
  1074.       color1 = degree/2+2 - color;
  1075.    else
  1076.       color1 = degree/2+degree+2 - color;
  1077.    j = xxstop-(x-xxstart);
  1078.    putcolor(       x, y, color+stripe) ;
  1079.    if (j < xdots)
  1080.       putcolor(    j, y, color1+stripe) ;
  1081.    if ((i=yystop-(y-yystart)) > iystop && i < ydots)
  1082.    {
  1083.       putcolor(    x, i, stripe + (degree+1 - color)%degree+1) ;
  1084.       if (j < xdots)
  1085.      putcolor(j, i, stripe + (degree+1 - color1)%degree+1) ;
  1086.    }
  1087. }
  1088.  
  1089. /* Do nothing plot!!! */
  1090. void _fastcall noplot(int x,int y,int color)
  1091. {
  1092. }
  1093.  
  1094.  
  1095.